package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryContoller {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;

    @PostMapping
    public R<String> save(  @RequestBody Category category){
        boolean saveCategory = categoryService.save(category);
        if (saveCategory) {
            return R.success("保存成功");
        }
        return R.error("保存失败");
    }
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        Page<Category> page1 = new Page(page,pageSize);
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(Category::getSort);
        return R.success(categoryService.page(page1,lqw));
    }
    @PutMapping
    public R update(@RequestBody Category category){
        boolean updateCheck = categoryService.updateById(category);
        if (updateCheck) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }
    @DeleteMapping
    public R delete(Long id){

//        boolean deleteCheck = categoryService.removeById(id);
//        if (deleteCheck) {
//            return R.success("删除成功");
//        }
//        categoryService.remove(id);
//        if (count==0) {
//            return R.success("删除成功");
//        }
        return categoryService.remove(id);
    }

    @GetMapping ("/list")
    public R list(Category category){

        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(null!=category.getType(),Category::getType,category.getType());
        lqw.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(lqw);
        return R.success(list);
    }

}
